package com.hs.excel.demos.listenter;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.hs.excel.demos.pojo.RequestParm;
import com.hs.excel.demos.service.transform.MysqlTransform;
import com.hs.excel.demos.service.transform.OracleTransform;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;

@Slf4j
public class InsertListenerNew extends AnalysisEventListener<Map<String, String>> {

    private RequestParm requestParm;
    private Map<Integer, String> head;

    private MysqlTransform mysqlTransform;
    private OracleTransform oracleTransform;


    public InsertListenerNew(RequestParm a, MysqlTransform mysqlTransform) {
        this.requestParm = a;
        this.mysqlTransform = mysqlTransform;
    }

    public InsertListenerNew(RequestParm a, OracleTransform oracleTransform) {
        this.requestParm = a;
        this.oracleTransform = oracleTransform;
    }

    @Override
    public void invoke(Map<String, String> data, AnalysisContext analysisContext) {
        if (this.requestParm.getInsertFlag()) {
            //StringBuffer  stringBuffer = new StringBuffer("insert into");
            //stringBuffer.append(" " + requestParm.getTableName() + "(");
            //
            //List<String> heads = new ArrayList<>(head.values());
            //for (String h : heads) {
            //    String s = h + ",";
            //    stringBuffer.append(s);
            //}
            //stringBuffer.deleteCharAt(stringBuffer.length() - 1);
            //stringBuffer.append(") ");
            //stringBuffer.append("VALUES(");
            //List<String> values = new ArrayList<>(data.values());
            //for (String filed : values) {
            //    if (filed == null) {
            //        String s = "'',";
            //        stringBuffer.append(s);
            //    } else {
            //        String s = "'" + filed + "'" + ",";
            //        stringBuffer.append(s);
            //    }
            //}
            //stringBuffer.deleteCharAt(stringBuffer.length() - 1);
            //stringBuffer.append("); ");
            //Comont.insertList.add(stringBuffer.toString());
            this.mysqlTransform.oneInsert(requestParm,head,data);
        }else {
            //单条
            this.mysqlTransform.entiretyMysqlhandle(data);
            //if (this.requestParm.getDbType().equals("mysql")) {
            //    entiretyMysqlhandle(data);
            //} else if (this.requestParm.getDbType().equals("oracle")){
            //    entiretyOracleHandle(data, head);
            //}

        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (!this.requestParm.getInsertFlag()){
            this.mysqlTransform.entiretyMysqlEnd(head, requestParm);
            //if (this.requestParm.getDbType().equals("mysql")) {
            //    entiretyMysqlEnd(head, requestParm);
            //} else if (this.requestParm.getDbType().equals("oracle")){
            //    entiretyOracleEnd();
            //}
        }
        //需要写入的文件的路径
        String filePath = "src/main/resources/" + requestParm.getTableName().toLowerCase() + ".sql";
        try {
            File file = new File(filePath);
            FileOutputStream fos = null;
            if (!file.exists()) {
                file.createNewFile();//如果文件不存在，就创建该文件
            }
            fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");//指定以UTF-8格式写入文件
            //遍历list
            for (String date : Comont.insertList) {
                //写
                osw.write(date);
                //每写入一个Map就换一行
                osw.write("\r\n");
            }
            //写入完成关闭流
            osw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("所有数据解析完成！");
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        this.head = headMap;
    }
}
